import sys, string, re, codecs

from yadict.util.sqlutil import openDatabase, createTable, insertTerm
from yadict.util.exceptionutil import printStack

def buildDatabaseNoPatch(pathFile, pathDatabase):
    return buildDatabase(pathFile, pathDatabase, False)

def buildDatabase(pathFile, pathDatabase, patchBabylon):
    
    # trick to print utf-8 data in stdout
    sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
    
    rows = loadFile(pathFile)
    
    if rows == None:
        print "no file loaded"
        return None

    # try to create database
    connection = openDatabase(pathDatabase)
    
    if connection == None:
        print "no database created"
        return None

    # pattern for digit
    myregexpr = re.compile("[A-Za-z0-9]")

    tables = {}
    ok = 0
    ko = 0
    patched = 0
    countRow = 0;
    
    # parsing file
    for row in rows:

        countRow = countRow + 1

        # del end of line
        row = row.replace("\n", "")
        
        if row.strip() == "" or row.strip()[0] == "#":
            # comment or nothing
            continue
  
        tokens = row.split("\t")
        
        if len(tokens) != 2 or tokens[0] == None or tokens[1] == None:
            # we have to reject invalid record
            print "rejected row " + str(countRow)
            ko = ko + 1
            continue
        
        # trimming
        tokens[0] = tokens[0].strip()
        tokens[1] = tokens[1].strip()
        
        if tokens[0] == "" or tokens[1] == "":
            print "rejected after strip row " +  str(countRow)
            ko = ko + 1
            continue
        
        # check if first char is a valid regexp
        retreg = myregexpr.search(tokens[0][0])
        if retreg == None or retreg.group() == "":
            print "rejected regexp row "  + str(countRow)
            ko = ko + 1
            continue

        if patchBabylon and tokens[0].count("$") >= 2 and tokens[0][len(tokens[0])-1] == "$":
            patched = patched + 1
            tokens[0] = tokens[0].split("$")[0]    
            
        #print tokens[0].strip() + "#" + tokens[1].strip()
        
        # numeric name goes into table "nums"
        tableName = None
        try:
            ret = int(tokens[0][0])
            # its a number
            tableName = "nums"
        except ValueError:
            # it's not a number
            tableName = tokens[0][0].lower()

        # we have to insert row in the table
        if tables.has_key(tableName) == False:
            print "create table " + tableName
            tables[tableName] = None
            # create table
            createTable(connection, tableName)

        # insert record
        try:
            insertTerm(connection, tableName, tokens[0], tokens[1])           
            #insertTerm(connection, tokens[0][0].lower().decode('iso-8859-1'), tokens[0].strip().decode('iso-8859-1'), tokens[1].strip().decode('iso-8859-1'))
            #insertTerm(connection, tokens[0][0].lower().encode('utf-8'), tokens[0].strip().encode('utf-8'), tokens[1].strip().encode('utf-8'))
            ok = ok + 1
        except:
            ko = ko + 1
            print "rejected exception row "  + str(countRow)
            print printStack(sys.exc_info())
    
    print "ok " + str(ok)
    print "patched " + str(patched)
    print "ko " + str(ko)

    return ok

def loadFile(pathFile):
    
    try:
        print "try to load file " + pathFile
        #f = open(pathFile, 'r')
        f = codecs.open(pathFile, 'r','utf-8')
        myrows = f.readlines()
        f.close()
        
        return myrows
        
    except IOError, (errno, strerror):
        print "Impossibile to open file"
        # go out
        sys.exit()
        

